package com.nobody.facepk.tools;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageTool {
	public static void main(String args[]) {   
		boolean ok = compressImage("test.gif", "test1.jpg", 200, 200, true);
		System.out.println(ok);
    }
	
	public static boolean compressImage(String srcFile, String desFile, int width, int height, boolean proportion) {   
		File src = new File(srcFile);
		return compressImage(src,desFile,width,height,proportion);
	}
	
	public static boolean compressImage(File file, String directoryFileName,   
            int width, int height, boolean proportion) {   
        boolean ret = false;   
        FileOutputStream fileOutputStream = null;   
        try {   
            if (file == null || directoryFileName == null) {   
                return ret;   
            }   
   
            fileOutputStream = new FileOutputStream(new File(directoryFileName));   
            Image image = ImageIO.read(file);   
            if (image.getWidth(null) == -1) {   
                return ret;   
            }   
   
            int oldWidth = image.getWidth(null);
            int oldHeight = image.getHeight(null);
            int newWidth = 0;   
            int newHeight = 0;   
   
            if (oldWidth==-1|| oldHeight ==-1 || oldWidth > width || oldHeight > height) {   
                if (proportion) {   
                    float rate1 = oldWidth / (float)width;   
                    float rate2 = oldHeight /  (float)height;   
                    float rate = rate1 > rate2 ? rate1 : rate2;   
                    newWidth = (int)(oldWidth / rate);   
                    newHeight = (int)(oldHeight / rate);   
                } else {   
                    newWidth = width;   
                    newHeight = height;   
                }   
            } else {   
                newWidth = oldWidth;   
                newHeight = oldHeight;   
            }   
   
            BufferedImage bufferedImage = new BufferedImage(newWidth,   
                    newHeight, BufferedImage.TYPE_INT_RGB);   
   
            bufferedImage.getGraphics().drawImage(   
                    image.getScaledInstance(newWidth, newHeight,   
                            image.SCALE_SMOOTH), 0, 0, null);   
   
            JPEGImageEncoder encoder = JPEGCodec   
                    .createJPEGEncoder(fileOutputStream);   
            encoder.encode(bufferedImage);   
            fileOutputStream.close();   
   
            ret = true;   
   
        } catch (Exception e) {   
            e.printStackTrace();   
        } finally {   
            if (fileOutputStream != null) {   
                try {   
                    fileOutputStream.close();   
                } catch (Exception e) {   
                    e.printStackTrace();   
                }   
            }   
        }   
        return ret;   
    } 
}
